<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/3/30
 * Time: 13:41
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Club extends Base {
    public function index(){
        if(request()->isAjax()){
            $club_name = input('club_name');
            $keyword = input('keyword');
            $where = [];
            if(isset($club_name) && $club_name != "")
            {
                $where['a.club_name'] = ['like', '%' . $club_name . '%'];
            }
            if(isset($keyword) && $keyword != "")
            {
                $where['uuu.user_username|uuu.user_phone'] = ['like', '%' . $keyword . '%'];
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_club')->alias('a')
                ->join('up_u_user uuu', 'uuu.user_id = a.club_user_id', 'left')
                ->join('up_gold ug','ug.gold_id = a.gold_id','left')
                ->where($where)
                ->count();// 获取总条数
            $lists =  Db::table('up_club')->alias('a')
                ->join('up_u_user uuu', 'uuu.user_id = a.club_user_id', 'left')
                ->join('up_gold ug','ug.gold_id = a.gold_id','left')
                ->field('a.*,uuu.user_username,uuu.user_phone,ug.gold_price')
                ->page($NowPage,$limits)
                ->order('club_id desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?club_id')){
            session('club_id',null);
        }
        return $this->fetch();
    }

    public function member(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $keyword = input('keyword');
            if(isset($keyword)&&$keyword!="")
            {
                $map['uuu.user_username|uuu.user_phone'] = ['like',"%" . $keyword . "%"];
            }
            $map['a.club_id'] = input('param.id');
            $map['a.m_status'] = 1;
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_club_member')
                ->alias ('a')
                ->join('up_u_user uuu','uuu.user_id = a.m_user_id','left')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_club_member')
                ->alias ('a')
                ->join('up_u_user uuu','uuu.user_id = a.m_user_id','left')
                ->field('a.*,uuu.user_username,uuu.user_phone')
                ->page($page, $limit)
                ->where($map)
                ->select();//计算总页面
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        if(!session('?club_id')){
            session('club_id',input('id'));
        }
        return $this->fetch();
    }
    //冻结
    public function frozen(){
        $id = input('param.id');
       $res =  Db::table('up_club')->where('club_id',$id)->setField('club_status',2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    public function allow(){
        $id = input('param.id');
        $res =  Db::table('up_club')->where('club_id',$id)->setField('club_status',1);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
}